package cn.neo.support.smartadapters.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;

import cn.neo.support.smartadapters.utils.ViewEventListener;

/**
 * <b>Name:</b>自定义SimpleDraweeView绑定视图<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b> 2017-01-11<br>
 * <b>Describe:</b> PLEASE TO FIXME...<br>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b><br>
 */
public abstract class BindableSimpleDraweeView<T> extends SimpleDraweeView implements BindableLayout<T> {

    protected ViewEventListener<T> viewEventListener;
    protected T item;
    protected int position;

    public BindableSimpleDraweeView(Context context) {
        super(context);
        initSimpleDraweeView();
    }

    public BindableSimpleDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public BindableSimpleDraweeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setLayoutParams(int w, int h) {
//        ViewGroup.LayoutParams.MATCH_PARENT
//        ViewGroup.LayoutParams.WRAP_CONTENT
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(w, h);
        setLayoutParams(layoutParams);
    }

    public abstract void initSimpleDraweeView() ;

    @Override
    public void bind(T item, int position) {
        this.item = item;
        this.position = position;
        bind(item);
    }

    @Override
    public abstract void bind(T item);

    @Override
    @Nullable
    public ViewEventListener<T> getViewEventListener() {
        return viewEventListener;
    }

    @Override
    public void setViewEventListener(ViewEventListener<T> viewEventListener) {
        this.viewEventListener = viewEventListener;
    }

    @Override
    public void notifyItemAction(int actionId, T theItem, View view) {
        if (viewEventListener != null) {
            viewEventListener.onViewEvent(actionId, theItem, position, view);
        }
    }

    public void notifyItemAction(int actionId, View view) {
        notifyItemAction(actionId, item, view);
    }

    public void notifyItemAction(int actionId) {
        notifyItemAction(actionId, item, this);
    }

    public T getItem() {
        return item;
    }

    public void setItem(T item) {
        this.item = item;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }
}
